<?php
/***
 * 描述：菜单管理模块
 * @category		
 * @package		Lib			
 * @subpackage	Action.System	
 * @author		wanght <whtaok@gmail.com>
 * @since		2013-4-6
 * @version		$Id: MenuAction.class.php 3 2013-10-14 02:49:41Z whtaok@gmail.com $
 */
class MenuAction extends SystemAction {
	
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		$this->usingModel="ShopUserMenu";
	}
	/**
	 * 显示菜单查询页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct($menu_id=0) {	
		//1.根据菜单类型取得菜单列表
		$mod	=	new ShopUserMenuModel();
		$mod->validForWhere('1,2,3');
		$menu	=	$mod->select();
		//2.生成菜单树型数组(common.php->list_to_tree)
		$menu_tree = list_to_tree($menu);
		$this->assign("menu_tree",$menu_tree);
		
		//3.生成菜单瀑布数组(common.php->list_to_treefalls)
		$menu_treefalls = list_to_treefalls($menu_tree);
		
		if($menu_id){
			//3.1显示指定菜单ID的菜单列表
			$levelTwoId = $menu_id;
			foreach ($menu_treefalls as $key=>$value){
				//1.查询的菜单ID == 当前遍历的菜单ID  显示一层菜单 $key == $menu_id
				//2.查询的菜单ID == 当前遍历的菜单PID 显示二层菜单 $value['menu_pid'] == $menu_id
				//3.二层的菜单ID == 当前遍历的菜单PID 显示三层菜单 $value['menu_pid'] == $levelTwoId
				if($menu_id == $key || $menu_id == $value['pid'] || $levelTwoId == $value['pid']){
					//如果当前菜单是二层菜单，那么存储二层菜单ID到临时变量中
					if($value['pid'] == $menu_id){
						$levelTwoId = $value['id'];
					}
					$menu_list[$key] = $value;
				}
				
			}
		}else{
			//3.1显示全部的查询菜单列表
			$menu_list = $menu_treefalls;
		}
		unset($menu_treefalls);
		unset($menu_tree);
		//4.设置页面变量
		
		$this->assign("menuId",$menu_id);
		$this->assign("menuList",$menu_list);
		
		$this->display();
	}
	
	/**
	 * 显示菜单添加页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */	
	public function showAddAct($menu_pid=0) {
		
		if($menu_pid){
			//子菜单：查询父菜单信息
			$mod =	new ShopUserMenuModel();
			$pmenu = $mod->find($menu_pid);
			if(!$pmenu){
				exit($this->dialog(STATUS_ERROR,'父菜单不存在，请重新操作'));
			}
		}else{
			//顶级菜单：初始化父菜单信息
			$pmenu['id']=$menu_pid;
			$pmenu['title']='顶级菜单';
		}
		$this->assign("pmenu",$pmenu);
		exit($this->fetch("add"));
	}
	
	/**
	 * 添加菜单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddAct() {	
		//保存菜单信息
		$mod =	new ShopUserMenuModel();
		if(!$mod->create()){
			$this->error($mod->getError());
		}
		$mod->add();
		$this->success('添加成功', U('index',array('menuId'=>$this->_param('pid'))));
	}
	/**
	 * 显示菜单编辑页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showUpdAct($menu_id) {
		//查询当前菜单信息
		$mod =	new ShopUserMenuModel();
		$menu = $mod->find($menu_id);
		
		//是否可以修改父级菜单
		if($menu['pid']){
			//查询父类菜单信息
			$pmenu = $mod->find($menu['pid']);
			//如果非顶级菜单，则查询和父级菜单同级的菜单列表
			$pmenu_list = $mod->where(array('pid'=>$pmenu['pid']))->select();
		}else{
			//如果是顶级菜单，则不可以修改父类菜单
			$pmenu_list[]=$menu;
		}
		
		//设置页面变量
		$this->assign("menu",$menu);
		$this->assign("pmenu",$pmenu);
		$this->assign("pmenuList",$pmenu_list);
		exit($this->fetch("upd"));
	}
	
	/**
	 * 编辑菜单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdAct() {		
		//保存菜单信息
		$mod =	new ShopUserMenuModel();
		if(!$mod->create()){
			$this->error($mod->getError());
		}
		$mod->save();
		$this->success('保存成功', U('index',array('menuId'=>I('pid'))));
	}

	/**
	 * 删除菜单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doDelAct() {
		//获得删除的菜单ID列表
		$id_list = array_values(I('id'));
		
		//获得全部菜单列表
		$mod =	new ShopUserMenuModel();
		$menu = $mod->find($id_list[0]);
		$menu_list = $mod->field(array('id','pid'))->select();
		$menu_tree = list_to_tree($menu_list);
		
		//获得一层菜单的删除ID列表及孩子ID列表
		$id_del_list = array();
		foreach($menu_tree as $key=>$value){
			if(in_array($key, $id_list)){
				//如果删除的是一层菜单，遍历该菜单的所有孩子节点
				$id_del_list[$key] = $key;
				$id_del_list =  $id_del_list + get_childrenkeys($value);
			}
		}
		
		//删除一层菜单及孩子菜单
		if($id_del_list){
			$id_del_list = array_values($id_del_list);
			$map1['id']  = array('in',$id_del_list);
			$mod->where($map1)->delete();
			
			//获得二层，三层菜单的删除ID列表（不包括一层菜单的删除ID及孩子ID列表）
			$id_del_list2 = array_diff($id_list,$id_del_list);
		}else{
			$id_del_list2 = $id_list;
		}

		//删除二层，三层菜单
		if($id_del_list2){
			//删除二层菜单/三层菜单
			$map2['id']  = array('in',$id_list);
			$mod->where($map2)->delete();
			//删除二层菜单的孩子菜单
			$map3['pid']  = array('in',$id_list);
			$mod->where($map3)->delete();
		}
		$this->success('删除成功', U('index'));
	}
	
	/**
	 * 删除菜单缓存
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function removeCacheAct($menu_type=null){
		F('menu_tree',NULL);
		$this->ajaxReturn("缓存删除成功");
	}
	
}

?>